'''
Created on Feb 6, 2010

@author: oabalbin
'''

from collections import deque

class paramfile:
    
    def __init__(self):
        self.bfrm_param={}
        self.para_name = []
        
    def parmeter_val(self, NLatentFactors, NObservations, NVariables, NDesignVariables, DataFile, HFile, EvolVarIn, EvolVarInFile,
                 EvolIncludeVariableThreshold=0.75, EvolIncludeFactorThreshold=0.75,
                  EvolMaximumFactors=10, EvolMaximumVariables=150):
        
        #Version 2.0
        #data section
        self.bfrm_param['NObservations'] = NObservations
        self.bfrm_param['NVariables'] = NVariables
        self.bfrm_param['NBinaryResponses'] = 0
        self.bfrm_param['NCategoricalResponses'] = 0
        self.bfrm_param['NSurvivalResponses'] = 0
        self.bfrm_param['NContinuousResponses'] = 0
        self.bfrm_param['NDesignVariables'] = NDesignVariables
        self.bfrm_param['NControlVariables'] = 0
        self.bfrm_param['NLatentFactors'] = NLatentFactors
        
        # input from program
        # If you want to use some of these parameters need to erase the comment in name
        self.bfrm_param['DataFile'] = DataFile 
        self.bfrm_param['HFile'] = HFile 
        self.bfrm_param['#ResponseMaskFile'] = 'ymask.txt'     
        self.bfrm_param['#XMaskFile'] = ''
    
        #prior section
        #model specification
        self.bfrm_param['ShapeOfB'] = 2    # Default value
        self.bfrm_param['NonGaussianFactors'] = 1 # Dirichlet process
        
        #prior Psi  # Note: email West about how to calculate this paramereters  Gamma(2,0.005). In the default file self.bfrm_param['PriorPsia'] = 10, self.bfrm_param['PriorPsib'] = 2
        self.bfrm_param['PriorPsia'] = 10
        self.bfrm_param['PriorPsib'] = 2
        self.bfrm_param['PriorSurvivalPsia'] = 2
        self.bfrm_param['PriorSurvivalPsib'] = 0.5
                
        #prior Rho
        self.bfrm_param['PriorRhoMean'] = 0.001
        self.bfrm_param['PriorRhoN'] = 200
        
        #prior Pi
        self.bfrm_param['PriorPiMean'] = 0.9
        self.bfrm_param['PriorPiN'] = 10
               
        #prior Tau
        self.bfrm_param['PriorTauDesigna'] = 5
        self.bfrm_param['PriorTauDesignb'] = 1
        self.bfrm_param['PriorTauResponseBinarya'] = 5
        self.bfrm_param['PriorTauResponseBinaryb'] = 1
        self.bfrm_param['PriorTauResponseCategoricala'] = 5
        self.bfrm_param['PriorTauResponseCategoricalb'] = 1
        self.bfrm_param['PriorTauResponseSurvivala'] = 5
        self.bfrm_param['PriorTauResponseSurvivalb'] = 1
        self.bfrm_param['PriorTauResponseContinuousa'] = 5
        self.bfrm_param['PriorTauResponseContinuousb'] = 1
        self.bfrm_param['PriorTauLatenta'] = 5
        self.bfrm_param['PriorTauLatentb'] = 1
        
        #priors on Intercept
        self.bfrm_param['PriorInterceptMean'] = 8
        self.bfrm_param['PriorInterceptVar'] = 100
        self.bfrm_param['PriorContinuousMean'] = 0
        self.bfrm_param['PriorContinuousVar'] = 1
        self.bfrm_param['PriorSurvivalMean'] = 2
        self.bfrm_param['PriorSurvivalVar'] = 1
        
        #evolving mode section
        self.bfrm_param['Evol'] = 1
        self.bfrm_param['EvolVarIn'] = EvolVarIn
        self.bfrm_param['EvolVarInFile'] = EvolVarInFile  
        self.bfrm_param['EvolIncludeVariableThreshold'] = EvolIncludeVariableThreshold #0.95
        self.bfrm_param['EvolIncludeFactorThreshold'] = EvolIncludeFactorThreshold #0.85
        self.bfrm_param['EvolMiniumVariablesInFactor'] = 5  # It can be adjusted if we want to include more variables.
        self.bfrm_param['#EvolMaximumVariablesPerFactor'] = 15 # It can be adjusted. Bigger numbers, less exploration of the factor space 
        self.bfrm_param['EvolMaximumFactors'] = EvolMaximumFactors #
        self.bfrm_param['EvolMaximumVariables'] = EvolMaximumVariables
        self.bfrm_param['EvolMaximumVariablesPerIteration'] = 5
        self.bfrm_param['InclusionMethod'] = 1
                
        #mcmc section
        self.bfrm_param['Burnin'] = 2000
        self.bfrm_param['nMCSamples'] = 5000
        
        #monitoring section
        self.bfrm_param['PrintIteration'] = 100
        
        #DP parameters
        self.bfrm_param['PriorAlphaa'] = 1
        self.bfrm_param['PriorAlphab'] = 1
        
        
        self.para_name = deque(['NObservations','NVariables','NBinaryResponses','NCategoricalResponses', 'NSurvivalResponses','NContinuousResponses','NDesignVariables',
                           'NControlVariables','NLatentFactors','DataFile','HFile','#ResponseMaskFile','#XMaskFile','ShapeOfB','NonGaussianFactors','PriorPsia',
                           'PriorPsib','PriorSurvivalPsia','PriorSurvivalPsib', 'PriorRhoMean','PriorRhoN','PriorPiMean','PriorPiN','PriorTauDesigna','PriorTauDesignb',
                            'PriorTauResponseBinarya','PriorTauResponseBinaryb','PriorTauResponseCategoricala','PriorTauResponseCategoricalb','PriorTauResponseSurvivala',
                            'PriorTauResponseSurvivalb','PriorTauLatenta','PriorTauLatentb','PriorInterceptMean','PriorInterceptVar','PriorContinuousMean', 'PriorContinuousVar',
                            'PriorSurvivalMean','PriorSurvivalVar', 'Evol','EvolVarIn','EvolVarInFile', 'EvolIncludeVariableThreshold', 'EvolIncludeFactorThreshold', 
                            'EvolMiniumVariablesInFactor', '#EvolMaximumVariablesPerFactor', 'EvolMaximumFactors', 'EvolMaximumVariables', 'EvolMaximumVariablesPerIteration', 'InclusionMethod',
                            'Burnin','nMCSamples','PrintIteration', 'PriorAlphaa', 'PriorAlphab'])
        
    def write_bfrmparam_file(self, outfile):
        """
        write the bfrm parameters file to the parameters folder 
        """
        section = {'ShapeOfB':'#model specification prior section', 'PriorPiMean':'#prior Rho',
                'PriorPiMean':'#prior Pi', 'PriorTauDesigna':'#prior Tau','PriorInterceptMean':'#priors on Intercept',
                'Evol':'#evolving mode section', 'Burnin':'#mcmc section', 'PrintIteration':'#monitoring section',
                'PriorAlphaa':'#DP parameters'}

        outfile.write('#Version 2.0'+'\n'+'#data section'+'\n')
        
        for i in range(len(self.para_name)):
            par = self.para_name.popleft()
            val = self.bfrm_param[par]            
            
            try:
                sect = section[par] 
                outfile.write(sect+'\n')
            except KeyError:
                i
            #print par+' = '+str(val)
            outfile.write(par+' = '+str(val)+'\n')



'''
bfrmpar = paramfile()
bfrmpar.parmeter_val(10, 200, 'myfile.txt', 24, 'hereis.txt')
bfrmpar.parmeter_val(10, 200, 'myfile.txt', 24, 'hereis.txt',
                 EvolIncludeVariableThreshold=0.75, EvolIncludeFactorThreshold=0.75,
                  EvolMaximumFactors=10, EvolMaximumVariables=150)
bfrmpar.write_bfrmparam_file('/home/oabalbin/projects/networks/prot_intc_db/')
'''